<?php
namespace appin\cms\controller\admin;

use appin\cms\Admin;
use appin\cms\validate\ArticleValidate;

class Articles extends Admin
{
    protected $model = \appin\cms\model\Articles::class;
    protected $validate = ArticleValidate::class;

    /**
     * 新闻模型
     * @return \think\response\View
     */
    public function index()
    {
        $column_id = $this->request->param('column_id', 0, 'intval');
        return $this->render('', ['column_id' => $column_id]);
    }

    /**
     * @return \think\response\Json
     */
    public function datas()
    {
        $column_id = $this->request->param('column_id', 0, 'intval');
        $data = $this->modelDatas($this->page, $this->limit, 'sort desc,id desc', 'column_id = '.$column_id);
        foreach($data['rows'] as &$row) {
            $row['column_title'] = $row->columns->title;
        }
        return json(['state' => 'success', 'total' => $data['total'], 'rows' => $data['rows']]);
    }

    /**
     * 文章编辑与新增
     * @return \think\response\View
     */
    public function save()
    {
        $id         = $this->request->param('id', 0, 'intval');
        $column_id  = $this->request->param('column_id', 0, 'intval');

        return $this->render('', [
            'column_id' => $column_id,
            'detail' => $this->model::find($id)
        ]);

    }
}